package indi.madoka.weeb.module.base.plugin;

import com.alibaba.fastjson.JSONObject;
import indi.madoka.weeb.annotations.Keyword;
import indi.madoka.weeb.annotations.Plugin;
import indi.madoka.weeb.bean.send.Sender;
import indi.madoka.weeb.bean.update.message.UpdateMessage;
import indi.madoka.weeb.enums.MatchType;
import indi.madoka.weeb.module.base.service.YosoroService;
import org.apache.commons.lang.StringUtils;

@Plugin("Yasoro Plugin")
public class YasoroPlugin {

    private final YosoroService yosoroService;

    public YasoroPlugin(YosoroService yosoroService) {
        this.yosoroService= yosoroService;
    }

    @Keyword(value = "#img", param = " [关键字仅支持英文]",matchType = MatchType.STARTS_WITH, description = "进行AI绘图")
    public void createImage(UpdateMessage updateMessage) {
        new Sender.Builder(updateMessage)
                .addText("少女祈祷中...")
                .build()
                .sendAll();
        String prompt = updateMessage.getRawMessage().substring(5).trim();
        JSONObject resultant = yosoroService.createImage(prompt);
        String url;
        String tags;
        if(!resultant.containsKey("result") || StringUtils.isEmpty((url = resultant.getString("result"))) ||
        !url.matches("https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]")) {
            new Sender.Builder(updateMessage)
                    .addText("省流：被ban了，换个关键词")
                    .build()
                    .sendAll();
            return;
        }
        tags = resultant.getString("prompt");
        new Sender.Builder(updateMessage)
                .addTextAtBegin(tags)
                .addImage(url)
                .build()
                .sendAll();
    }

    @Keyword(value = "#抽卡", param = " [常驻|武器|UPA|UPB]",matchType = MatchType.STARTS_WITH, description = "进行抽卡")
    public void draw(UpdateMessage updateMessage) {
        String prompt = updateMessage.getRawMessage().substring(4).trim();
        if("UPA".equals(prompt))
            prompt = "角色A";
        if("UPB".equals(prompt))
            prompt = "角色B";
        String url = yosoroService.drawCard(prompt, updateMessage.getUserId());
        if(StringUtils.isEmpty(url) || url.endsWith("error.png")) {
            new Sender.Builder(updateMessage)
                    .addText("省流：抽卡失败了")
                    .build()
                    .sendAll();
            return;
        }
        new Sender.Builder(updateMessage)
                .addTextAtBegin("本次的抽卡结果是")
                .addImage(url)
                .build()
                .sendAll();
    }

    @Keyword(value = "#查询", param = " [角色|武器]",matchType = MatchType.STARTS_WITH, description = "查询抽卡详情")
    public void seekCharacter(UpdateMessage updateMessage) {
        String tag = updateMessage.getRawMessage().substring(4).trim();
        if(!tag.equals("角色") && !tag.equals("武器")) {
            new Sender.Builder(updateMessage)
                    .addText("参数错误")
                    .build()
                    .sendAll();
            return;
        }
        String record = tag.equals("角色") ? yosoroService.getCharacterRecord(updateMessage.getUserId()) :
                yosoroService.getWeaponRecord(updateMessage.getUserId());
        if(StringUtils.isEmpty(record)) {
            new Sender.Builder(updateMessage)
                    .addText("还没有抽卡记录")
                    .build()
                    .sendAll();
            return;
        }
        new Sender.Builder(updateMessage)
                .addText(record)
                .build()
                .sendAll();
    }

}
